package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.SubjectThreeReg;
import com.gljx.web.Entity.SubjectThreeRegentry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.SubjectThreeRegMapper;
import com.gljx.web.dao.SubjectThreeRegentryMapper;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.SubjectThreeRegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class SubjectThreeRegServiceImpl extends CommonDao implements SubjectThreeRegService {

	@Autowired
	private SubjectThreeRegMapper subjectThreeRegMapper;
	
	@Autowired
	private SubjectThreeRegentryMapper  subjectThreeRegentryMapper;

	@Autowired
	private CustomLogService customLogService;

	//导入activiti工具
	ActitiviUtil activitiUtil = new ActitiviUtil();
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SubjectThreeRegMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SubjectThreeRegMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selectFnumber(companyId);
	}

	@Override
	public int addSubjectThreeReg(User user,SubjectThreeReg subjectThreeReg, String[] subfid, String[] subfseq,
			String[] subcfzkzmbh, String[] subcfnumber, String[] subcfkmsbh, String[] subfnameL2,
			String[] substufnumber, String[] subcfidcardno, String[] subcfsex, String[] subcfsjhm, String[] subcflxdh,
			String[] subcftjrName, String[] subcfsqzjcxdh, String[] subcfjiaolianid, String [] subjlname,String[] subcfchepai,
			String[] subcfchexing, String[] subcflocal, String[] subcfkscs, String[] subcfckbk, String[] subcfbkfcs,
			String[] subcfbkfze, String[] subcfbxwx, String[] subcfkmyjgsj, String[] subcfkmehgsj,
			String[] subcfkmsbjgsj, String[] subcfclazz, String[] subcfysje, String[] subcfyhje, String[] subcfyisje,
			String[] subcfqfje, String[] subcfdajj, String[] subcfjnbjj,String [] subcfremark) {
		// TODO Auto-generated method stub
		int row =0;
		//增加父类 科目三考试登记
		row +=subjectThreeRegMapper.insertSelective(subjectThreeReg);
		//增加子类 科目三考试登记明细
		for (int i = 0; i < subfid.length; i++) {
			SubjectThreeRegentry subjectThreeRegentry=new SubjectThreeRegentry();
        	subjectThreeRegentry.setFid(GetFid.UUIDIntoFid());
        	subjectThreeRegentry.setFseq(Long.valueOf(subfseq[i]));
        	subjectThreeRegentry.setFparentid(subjectThreeReg.getFid());
        	subjectThreeRegentry.setCfstudentid(subfid[i]);
        	subjectThreeRegentry.setCfzkzmbh(subcfzkzmbh[i]);
        	subjectThreeRegentry.setStudentName(subfnameL2[i]);
        	subjectThreeRegentry.setCfstudentno(substufnumber[i]);
        	subjectThreeRegentry.setCfidcardno(subcfidcardno[i]);
        	subjectThreeRegentry.setCfsex(Long.valueOf(subcfsex[i]));
        	subjectThreeRegentry.setCfmobile(subcfsjhm[i]);
        	subjectThreeRegentry.setCfphone(subcflxdh[i]);
        	subjectThreeRegentry.setCfrjr(subcftjrName[i]);
        	subjectThreeRegentry.setCfsqcx(subcfsqzjcxdh[i]);        	
        	subjectThreeRegentry.setCfjiaolianid(subcfjiaolianid[i]);
        	subjectThreeRegentry.setJlzname(subjlname[i]);
        	subjectThreeRegentry.setCfchepa(subcfchepai[i]);
        	subjectThreeRegentry.setCfchexing(subcfchexing[i]);
        	subjectThreeRegentry.setCfbdyd(Long.valueOf(subcflocal[i]));
        	subjectThreeRegentry.setCfkscs(Long.valueOf(subcfkscs[i]));
        	subjectThreeRegentry.setCfckbk(subcfckbk[i]);
        	subjectThreeRegentry.setCfbxwx(subcfbxwx[i]);
        	subjectThreeRegentry.setCfkmyjgsj(subcfkmyjgsj[i]);
        	subjectThreeRegentry.setCfnumber(subcfnumber[i]);
        	subjectThreeRegentry.setCfkmsbh(subcfkmsbh[i]);
        	subjectThreeRegentry.setCfkmyjgsj(subcfkmyjgsj[i]);
        	subjectThreeRegentry.setCfkmehgsj(subcfkmehgsj[i]);
        	subjectThreeRegentry.setCfbjgsj(subcfkmsbjgsj[i]);
        	subjectThreeRegentry.setCfclazz(subcfclazz[i]);
        	subjectThreeRegentry.setCfbkfcs(Long.valueOf(subcfbkfcs[i]));
        	subjectThreeRegentry.setCfbkfze(new BigDecimal(subcfbkfze[i]));
        	subjectThreeRegentry.setCfysje(new BigDecimal(subcfysje[i]));
        	subjectThreeRegentry.setCfyhje(new BigDecimal(subcfyhje[i]));
        	subjectThreeRegentry.setCfyisje(new BigDecimal(subcfyisje[i]));
        	subjectThreeRegentry.setCfqfje(new BigDecimal(subcfqfje[i]));
        	subjectThreeRegentry.setCfremark(subcfremark[i]);
        	//增加
        	row +=subjectThreeRegentryMapper.insertSelective(subjectThreeRegentry);
			//添加学员服务日志
			customLogService.addLog("考试管理",  "新增了学员:"+subjectThreeRegentry.getCfstudentno()+subjectThreeRegentry.getStudentName()+
					",科目三考试登记!车型:"+subjectThreeRegentry.getCfsqcx()+",教练:"+subjectThreeRegentry.getJlzname()+",考试日期:"+subjectThreeReg.getFbizdate()+",单据编号:"+subjectThreeReg.getFnumber(), subjectThreeRegentry.getCfstudentid(), user.getFid());
		}
		return row;
	}

	@Override
	public int deleteByPrimaryKey(String fid) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public SubjectThreeReg selSubjectThreeRegByFid(String fid) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selectByPrimaryKey(fid);
	}

	@Override
	public int updateSubjectThreeReg(SubjectThreeReg subjectThreeReg) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.updateByPrimaryKeySelective(subjectThreeReg);
	}

	@Override
	public int updateSubjectThreeReg(User user, SubjectThreeReg subjectThreeReg, String[] subfid, String[] subfseq,
									 String[] subcfzkzmbh, String[] subcfnumber, String[] subcfkmsbh, String[] subfnameL2,
									 String[] substufnumber, String[] subcfidcardno, String[] subcfsex, String[] subcfsjhm, String[] subcflxdh,
									 String[] subcftjrName, String[] subcfsqzjcxdh, String[] subcfjiaolianid, String [] subjlname,String[] subcfchepai,
									 String[] subcfchexing, String[] subcflocal, String[] subcfkscs, String[] subcfckbk, String[] subcfbkfcs,
									 String[] subcfbkfze, String[] subcfbxwx, String[] subcfkmyjgsj, String[] subcfkmehgsj,
									 String[] subcfkmsbjgsj, String[] subcfclazz, String[] subcfysje, String[] subcfyhje, String[] subcfyisje,
									 String[] subcfqfje, String[] subcfdajj, String[] subcfjnbjj,String [] subcfremark) {
		// TODO Auto-generated method stub
		int row =0;
		if(subjectThreeReg.getFid()!=null && !subjectThreeReg.getFid().equals("")){
			//修改父类	  	      
			row +=subjectThreeRegMapper.updateByPrimaryKeySelective(subjectThreeReg);
			//删除父类对应的子类
			row += subjectThreeRegentryMapper.deleteByParentId(subjectThreeReg.getFid());
			//增加子类
			for (int i = 0; i < subfid.length; i++) {
				SubjectThreeRegentry subjectThreeRegentry=new SubjectThreeRegentry();
	        	subjectThreeRegentry.setFid(GetFid.UUIDIntoFid());
	        	subjectThreeRegentry.setFseq(Long.valueOf(subfseq[i]));
	        	subjectThreeRegentry.setFparentid(subjectThreeReg.getFid());
	        	subjectThreeRegentry.setCfstudentid(subfid[i]);
	        	subjectThreeRegentry.setCfzkzmbh(subcfzkzmbh[i]);
	        	subjectThreeRegentry.setStudentName(subfnameL2[i]);
	        	subjectThreeRegentry.setCfstudentno(substufnumber[i]);
	        	subjectThreeRegentry.setCfidcardno(subcfidcardno[i]);
	        	subjectThreeRegentry.setCfsex(Long.valueOf(subcfsex[i]));
	        	subjectThreeRegentry.setCfmobile(subcfsjhm[i]);
	        	subjectThreeRegentry.setCfphone(subcflxdh[i]);
	        	subjectThreeRegentry.setCfrjr(subcftjrName[i]);
	        	subjectThreeRegentry.setCfsqcx(subcfsqzjcxdh[i]);        	
	        	subjectThreeRegentry.setCfjiaolianid(subcfjiaolianid[i]);
	        	subjectThreeRegentry.setJlzname(subjlname[i]);
	        	subjectThreeRegentry.setCfchepa(subcfchepai[i]);
	        	subjectThreeRegentry.setCfchexing(subcfchexing[i]);
	        	subjectThreeRegentry.setCfbdyd(Long.valueOf(subcflocal[i]));
	        	subjectThreeRegentry.setCfkscs(Long.valueOf(subcfkscs[i]));
	        	subjectThreeRegentry.setCfckbk(subcfckbk[i]);
	        	subjectThreeRegentry.setCfbxwx(subcfbxwx[i]);
	        	subjectThreeRegentry.setCfkmyjgsj(subcfkmyjgsj[i]);
	        	subjectThreeRegentry.setCfnumber(subcfnumber[i]);
	        	subjectThreeRegentry.setCfkmsbh(subcfkmsbh[i]);
	        	subjectThreeRegentry.setCfkmyjgsj(subcfkmyjgsj[i]);
	        	subjectThreeRegentry.setCfkmehgsj(subcfkmehgsj[i]);
	        	subjectThreeRegentry.setCfbjgsj(subcfkmsbjgsj[i]);
	        	subjectThreeRegentry.setCfclazz(subcfclazz[i]);
	        	subjectThreeRegentry.setCfbkfcs(Long.valueOf(subcfbkfcs[i]));
	        	subjectThreeRegentry.setCfbkfze(new BigDecimal(subcfbkfze[i]));
	        	subjectThreeRegentry.setCfysje(new BigDecimal(subcfysje[i]));
	        	subjectThreeRegentry.setCfyhje(new BigDecimal(subcfyhje[i]));
	        	subjectThreeRegentry.setCfyisje(new BigDecimal(subcfyisje[i]));
	        	subjectThreeRegentry.setCfqfje(new BigDecimal(subcfqfje[i]));
				subjectThreeRegentry.setCfremark(subcfremark[i]);
	        	//增加
	        	row += subjectThreeRegentryMapper.insertSelective(subjectThreeRegentry);
				//添加学员服务日志
				customLogService.addLog("考试管理",  "修改了学员:"+subjectThreeRegentry.getCfstudentno()+subjectThreeRegentry.getStudentName()+
						",科目三考试登记!车型:"+subjectThreeRegentry.getCfsqcx()+",教练:"+subjectThreeRegentry.getJlzname()+",考试日期:"+subjectThreeReg.getFbizdate()+",单据编号:"+subjectThreeReg.getFnumber(), subjectThreeRegentry.getCfstudentid(), user.getFid());
			}
		}
		return row;
	}

	@Override
	public List<SubjectThreeReg> selSubjectThreeRegByfbizdate(String companyid,String fbizdate, String cfcartype) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selSubjectThreeRegByfbizdate(companyid,fbizdate, cfcartype);
	}

	@Override
	public List<SubjectThreeReg> selSubjectThreeSumEnterPersons(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selSubjectThreeSumEnterPersons(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectThreeReg> selSubjectThreeResultPassExamRate(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selSubjectThreeResultPassExamRate(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectThreeReg> selSubjectThreeSumEnterPersonsByMonth(String companyId, String beginTime,
			String endTime) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selSubjectThreeSumEnterPersonsByMonth(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectThreeReg> selSubjectThreeResultPassExamRateByMonth(String companyId, String beginTime,
			String endTime) {
		// TODO Auto-generated method stub
		return subjectThreeRegMapper.selSubjectThreeResultPassExamRateByMonth(companyId, beginTime, endTime);
	}

	@Override
	public String selectAuditPerson(User user,Object fnumber) {
		//2.查询
		String result = activitiUtil.getTaskStateByUser(
				ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid()) ? ActivitiKey.SUBJECTTHREEREG_KEY_GL : ActivitiKey.SUBJECTTHREEREG_KEY_NC,
				fnumber                  //单据
		);
		if (activitiUtil.notTask.equals(result)) {
			System.out.println("该单据没有对应流程");
			return "noTask";
		} else if (activitiUtil.FINISH.equals(result)) {
			System.out.println("流程已结束");
			return "finish";
		}//剩下两种是该任务进行中
		else if (activitiUtil.notAssign.equals(result)) {
			System.out.println("经办人不匹配");
			return "noAssign";
		} else if (activitiUtil.ISPOWER.equals(result)) {
			System.out.println("当前用户可以审核该单据");
			return "power";
		} else {
			return "failed";
		}
	}

	@Override
	public List<SubjectThreeReg> selectSubjectThreeReg(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return subjectThreeRegMapper.selectSubjectThreeReg(map);
	}

}
